package com.cloud.lan.service.sms;

import com.alibaba.fastjson.JSON;
import com.cloud.lan.api.dto.req.SmsTemplateReq;
import com.cloud.lan.api.dto.resp.SmsTemplateResp;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.ListResponse;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateHandleRes;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateModel;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateQueryReq;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateQueryRes;
import com.cloud.lan.integration.submail.sms.service.SmsTemplateService;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by zhouquan on 2022/4/3.
 */
@Slf4j
@Service
public class SmsTemplateManageService {

    @Autowired
    private SmsTemplateService smsTemplateService;

    public ListResponse manageList(SmsTemplateReq templateReq){
        log.info("短信模板.列表查询.req:[{}]", JSON.toJSONString(templateReq));
        ListResponse listResponse = new ListResponse();
        SmsTemplateQueryReq queryReq = new SmsTemplateQueryReq();
        queryReq.setTemplateId(templateReq.getTemplateId());
        if(templateReq.getPageIndex()>0){
            queryReq.setOffset(String.valueOf(templateReq.getPageIndex()-1));
        }
        SmsTemplateQueryRes queryRes = smsTemplateService.query(queryReq);
        List<SmsTemplateResp> respList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(queryRes.getTemplates())){
            queryRes.getTemplates().stream().forEach(te->{
                try{
                    SmsTemplateResp resp = new SmsTemplateResp();
                    BeanUtils.copyProperties(resp,te);
                    respList.add(resp);
                }catch (Exception e){
                    log.error("短信模板.转换异常",e);
                }
            });
        }
        listResponse.setData(respList);
        listResponse.setNums(respList.size());
        log.info("短信模板.列表查询.res:[{}]", JSON.toJSONString(listResponse));
        return listResponse;
    }

    public BaseResponse merge(SmsTemplateReq templateReq){
        log.info("短信模板.merge.req:[{}]", JSON.toJSONString(templateReq));
        SmsTemplateModel model = new SmsTemplateModel();
        model.setTemplateId(templateReq.getTemplateId());
        model.setSmsTitle(templateReq.getSmsTitle());
        model.setSmsSignature(templateReq.getSmsSignature());
        model.setSmsContent(templateReq.getSmsContent());
        if(StringUtils.isBlank(templateReq.getTemplateId())){
            //create
            SmsTemplateHandleRes handleRes = smsTemplateService.create(model);
            BaseResponse baseResponse = BaseResponse.success();
            baseResponse.setData(handleRes);
            return baseResponse;
        }else{
            //update
            SmsTemplateHandleRes handleRes = smsTemplateService.update(model);
            BaseResponse baseResponse = BaseResponse.success();
            handleRes.setTemplateId(model.getTemplateId());
            baseResponse.setData(handleRes);
            return baseResponse;
        }
    }

    public BaseResponse del(SmsTemplateReq templateReq){
        log.info("短信模板.del.req:[{}]", JSON.toJSONString(templateReq));
        SmsTemplateModel model = new SmsTemplateModel();
        model.setTemplateId(templateReq.getTemplateId());
        SmsTemplateHandleRes handleRes = smsTemplateService.del(model);
        BaseResponse baseResponse = BaseResponse.success();
        handleRes.setTemplateId(model.getTemplateId());
        baseResponse.setData(handleRes);
        return baseResponse;
    }

}
